home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Standard struct layout in C++?
- Date: Sat, 20 Apr 1996 15:23:37 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4lavga$5m6@news.halcyon.com>
- References: <4l739o$gt4$2@mhade.production.compuserve.com>
- NNTP-Posting-Host: blv-pm2-ip16.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- anw <102044.3670@CompuServe.COM> wrote:
-
- >Does C++ guarantee any data layout in classes (or structs)? I
- >can't find any reference in Ellis and Stroustrup (the ARM). The
- >problem I am trying to solve is reading and writing blocks of data
- >that represent classes or structures; e.g.:
-
- >class foo;
-
- >read(fd, buffer, sizeof(foo));
-
- >Thanx,
-
- >anw
-
- I believe this is a risky habit, largely because it's a maintenance
- woe:
-
- Unless you specifiy a structure packing (#pragma pack in MSVC), you
- don't know how your data may be aligned, 8-byte boundaries on a RISC
- chip, 2-byte on Win3.1, whatever, so you have to be cautious.
-
- If the structs ever contain methods, the vtable could go ahead or
- behind the data, depending on the compiler (and maybe on the version
- of the compiler), and when such structs appear in multiple inheritence
- hierarchies, and esp multiple-virtual inheritence, there's no telling
- which compiler has put what vtable where.
-
- You may also chew up more disk-space than you wanted because your
- saving out padding and possibly vtables.
-
- Some folks have taken to also comparing their structs with memcmp()
- instead of member-by-member. Both the above problems come into play:
- esp packing because your globals pad with zero and your locals pad
- with random garbage.
-
- It's so much nicer to write a method for your data to persist (and
- compare) member-by-member and ask the base-class to do likewise.
-
- --Norm
-
-
-
-
-